﻿
using Controller.i18n.entity;
using Controller.i18n.plc.crane;
using HBase;
using hCommand;
using hconnect;
using Net.http;
using System;
using System.Collections.Generic;

namespace Controller
{
    [RoutePrefix("Crane")]
    public class CraneServer : BaseApiController
    {
        ///Crane/craneStates/0
        [HttpGet, Route("craneStates/{craneId}")]
        public BaseResult craneStates(int craneId)
        {
            CraneStatus rs = (CraneStatus)StatusCache.getStatus<CraneSynchro>(craneId);
            if (rs == null)
            {
                return BaseResult.Ok(new CraneStatus());
            }
            return BaseResult.Ok(rs);
        }

        ///Crane/ClearTaskState/craneId/forkNo
        public bool ClearTaskState(List<String> param)
        {
            return false;
        }

        [HttpGet, Route("sendTask/{CraneId}/{forkNo}/{taskType}/{fromPlcShelf}/{fromPlcRow}/{fromPlcCol}/{toPlcShelf}/{toPlcRow}/{toPlcCol}/{src}/{des}")]
        public bool sendTask(int CraneId, int forkNo, int taskType, int fromPlcShelf, int fromPlcRow, int fromPlcCol, int toPlcShelf, int toPlcRow, int toPlcCol,int src,int des)
        {
            TaskCrane model = new TaskCrane();
            model.taskNo = TaskUtil.getTaskNo();
            model.craneId = (byte)CraneId;
            model.forkNo = (byte)forkNo;
            model.src = src;
            model.des = des;    
            model.sz = fromPlcShelf;
            model.sx = fromPlcRow;
            model.sy = fromPlcCol;
            model.dz = toPlcShelf;
            model.dx = toPlcRow;
            model.dy = toPlcCol;
            CraneConnect connect = TaskPool.get<CraneConnect>(CraneId);
            bool result = connect.sendTask(model, 0);
            return result;
        }
        [HttpGet, Route("sendTaskloc/{src}/{des}")]
        public String sendTaskloc( int src, int des) {

            CoreWhLoc StartPoint = CoreWhLoc.getCoreWhLoc(Math.Abs(src));
            if (StartPoint == null) {
                return "起点位置异常";
            }
            CoreWhLoc EndPoint = CoreWhLoc.getCoreWhLoc(Math.Abs(des));
            if (EndPoint == null) {
                return "目标位置异常";
            }
            TaskCrane model = new TaskCrane();
            model.taskNo = TaskUtil.getTaskNo();
           
            model.changeSrc(src);
            model.changeDes(des);
            CraneConnect connect = TaskPool.get<CraneConnect>(model.craneId);
            bool result = connect.sendTask(model, 0);
            return result+"";
        }

        [HttpGet, Route("EmergencyStop/{CraneId}")]
        public bool EmergencyStop(int CraneId)
        {
            CraneConnect connect = TaskPool.get<CraneConnect>(CraneId);
            connect.EmergencyStop();
            return true;
        }

        [HttpGet, Route("Reset/{CraneId}")]
        public bool Reset(int CraneId)
        {
            CraneConnect connect = TaskPool.get<CraneConnect>(CraneId);
            //connect.Reset();
            return true;
        }
        [HttpGet, Route("reset/{CraneId}")]
        public string reset(int CraneId)
        {
            CraneConnect connect = TaskPool.get<CraneConnect>(CraneId);
            connect.reset();
            return "复位成功";
        }



        ///Crane/craneStates/craneId/forkNo/src/des/actionid
        public bool Send_SEMI_Task(List<String> param)
        {
            return false;
        }

        public List<TaskCrane> getTaskCraneBycompleteId(List<String> param)
        {
            long completeId = long.Parse(param[2]);
            return null;
        }



        [HttpGet, Route("ClearCraneFault/{CraneId}")]
        public BaseResult ClearCraneFault(int CraneId)
        {

            CraneStatus status = (CraneStatus)StatusCache.getStatus<CraneSynchro>(CraneId);
            if (status != null)
            {
                status.clearFault();
                bool b = status.isfault();
                return BaseResult.Ok(true);
            }
            return BaseResult.Ok(false);
        }
        [HttpGet, Route("clearLed")]
         public BaseResult clearLed() {
            List<int> l=new List<int>() { 111,114,121,124,131,132,141,142};
            foreach(int id in l){
                TaskPool.get<KanBanConnect>(id).clearLine1();
            }
          
            return BaseResult.Ok(false);
        }



        [HttpGet, Route("test4/{CraneId}/{value}/{index}")]
        ///Carrier/resetAction/path
        public String testT(int CraneId, int value, int index)
        {

            CraneConnect connect = TaskPool.get<CraneConnect>(CraneId);
            connect.set4(index, value);


            return "";
        }
        [HttpGet, Route("test2/{CraneId}/{value}/{index}")]
        ///Carrier/resetAction/path
        public String testS(int CraneId, int value, int index)
        {

            CraneConnect connect = TaskPool.get<CraneConnect>(CraneId);
            connect.set2(22, 1);
            connect.set2(24, 5);
            connect.set2(0, 2304);

            //  connect.set2(index ,value);


            return "";
        }


    }
}
